import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        item: {},
        type: 0,                //播放类型，随机单曲顺序
        praise: [],             //点赞列表
        audioplay: false,       //播放器播放、暂停
        alertFlag: false,       //我的弹窗
        PublicAlertFlag: false, //列表弹窗
        audioFlag: false,       //小播放器弹窗
        audioranFlag: false,    //大播放器弹窗
        alertlist: [],          //我的弹窗列表
        img: '',                //图片地址
        sings: [],              //歌曲地址
        audioIndex: 0,          //当前播放下标
        audioUrl: '',           //当前播放音频路径
        perc: 0,                //播放百分比
        dtime: '',              //当前播放时长
        audioTime: ''            //当前音频总时长
    },
    mutations: {
        setType(state){
            state.type ++ 
            if(state.type === 3) state.type = 0
        },
        getpraise(state, obj){     
            state.praise = JSON.parse(localStorage.getItem('praise')) || []    
            if(obj.zan){
                state.praise.push(obj)
            }else{
                state.praise = state.praise.filter(val => val.zan === false)
            }
            localStorage.setItem('praise', JSON.stringify(state.praise))
        },
        setaudioplay(state){
            state.audioplay = !state.audioplay
        },
        setperc(state, { dtime, ztime}){
            state.perc = dtime / ztime * 100
        },
        setdtime(state, time){
            let ss = Math.floor(time % 60)
            let ff = Math.floor(time / 60)
            ss = ss >= 10 ? ss : '0' + ss 
            ff = ff >= 10 ? ff : '0' + ff 
            state.dtime = `${ff}:${ss}`
        },
        setaudioTime(state, time){
            let ss = Math.floor(time % 60)
            let ff = Math.floor(time / 60)
            ss = ss >= 10 ? ss : '0' + ss 
            ff = ff >= 10 ? ff : '0' + ff 
            state.audioTime = `${ff}:${ss}`
        },
        setaudioranFlag(state, type){
            state.audioranFlag = type
        },
        getAudiourl(state, str){
            state.audioUrl = str
        },
        setindexaudio(state, i) {    
            if(i === 1) {
                console.log(state.audioIndex);
                state.audioIndex = state.audioIndex + 1
                if(state.audioIndex >= state.sings.length) state.audioIndex = 0
                console.log(state.audioIndex);
            }
            if(i === 3) {
                state.audioIndex = state.audioIndex - 1
                if(state.audioIndex < 0)state.audioIndex = state.sings.length - 1
            }
            if(i === 2) {
                state.audioIndex = Math.floor(Math.random() * state.sings.length)
            }
            if(i === 0) {
                state.audioIndex = state.audioIndex
            }           
        },
        getAudio(state){
            state.audioFlag = true 
        },
        setimg(state, str){
            state.img = str
        },
        gititem(state, payload){
            state.item = payload
        },
        changeAlert(state,pay){//改变alert显示隐藏
            state.alertFlag = pay
        },
        getalertlist(state,payload){//获取alert数据
            state.alertlist = payload;
        },
        setPublicAlertFlag(state,pay){
            state.PublicAlertFlag = pay
        },
        getsings(state,pay){
            let num = state.sings.findIndex(val => val.mid === pay.mid)
            if(num < 0) {
                state.sings.push(pay)
                localStorage.setItem('sings', JSON.stringify(state.sings))
            }
        },
        remove(state,payload){
            console.log(state.sings);
            console.log(payload);
            state.sings = state.sings.filter(val => val.mid !== payload.mid)
        },
        clearSings(state){
            state.sings = []
        }
    },
    actions: {
    },
    modules: {
    }
})